
// #include "kal_general_types.h"
#include "os_config.h"
// #include "globle_include.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>




#include "os_timer.h"


extern int mytimer_task_isr_enqueue_msg(TimerId_id tid,unsigned int ms,
	kal_timer_func_ptr cfun,void *param,bool isauto);


#if defined(RT_USING_ULOG)
#define LOG_TAG              "OStimer"
#define LOG_LVL              LOG_LVL_WARNING // LOG_LVL_INFO
#include <ulog.h>

#define TIMER_LOG       LOG_D
#define TIMER_LOG2		 LOG_I	
#define TIMER_LOG3		 LOG_E


#else

#define TIMER_LOG   trace_print
#define TIMER_LOG2(...)
#define TIMER_LOG3(...)

#endif



#ifdef ENABLE_VCOM_USMART	
#include "usmart.h"

static unsigned int s_timer_max_tick;
static unsigned int s_last_fun_sum_tick;
static void*    s_timer_max_tick_fun_addr;
static void*    s_last_fun_addr;

void mytimer_static_info(void)
{

	vcom_usmart_printf("mytimer:%d;%p;last:%d;%p\n",s_timer_max_tick,s_timer_max_tick_fun_addr,
		s_last_fun_sum_tick,s_last_fun_addr);
}
#endif

#if defined(__FreeRTOS__)

void timer_commom_callback( TimerHandle_t xTimer)
{
	TimerId_id tidptr;
	#ifdef ENABLE_VCOM_USMART
	unsigned int tick;
	#endif
	
	tidptr = (TimerId_id)pvTimerGetTimerID(xTimer);//xTimer->pvTimerID;

	 if(tidptr != NULL && tidptr->callfuntion != NULL){
		//tidptr->callfuntion((void *)tidptr);
		//sysprintf("@%x+ ",tidptr->callfuntion);
		#ifdef ENABLE_VCOM_USMART
		tick = kal_get_systicks();
		s_last_fun_sum_tick = 0;
		s_last_fun_addr = tidptr->callfuntion;
		#endif
		tidptr->callfuntion(tidptr->param_ptr);

		#ifdef ENABLE_VCOM_USMART
		tick = kal_ticks_to_milli_secs(kal_get_systicks() - tick);

		s_last_fun_sum_tick = tick;
		if(tick > s_timer_max_tick){
			s_timer_max_tick = tick;
			s_timer_max_tick_fun_addr = tidptr->callfuntion;
		}
		#endif
		//sysprintf("&%x-\n",tidptr->callfuntion);
  	 }
}
#elif defined(__RTT_OS__)

static void timer_commom_callback( void * p)
{
	TimerId_id tidptr;
	#ifdef ENABLE_VCOM_USMART
	unsigned int tick;
	#endif
	
	tidptr = (TimerId_id)p;//xTimer->pvTimerID;

	 if(tidptr != NULL && tidptr->callfuntion != NULL){
		//tidptr->callfuntion((void *)tidptr);
		//sysprintf("@%x+ ",tidptr->callfuntion);
		#ifdef ENABLE_VCOM_USMART
		tick = kal_get_systicks();
		s_last_fun_sum_tick = 0;
		s_last_fun_addr = tidptr->callfuntion;
		#endif
		tidptr->callfuntion(tidptr->param_ptr);

		#ifdef ENABLE_VCOM_USMART
		tick = kal_ticks_to_milli_secs(kal_get_systicks() - tick);

		s_last_fun_sum_tick = tick;
		if(tick > s_timer_max_tick){
			s_timer_max_tick = tick;
			s_timer_max_tick_fun_addr = tidptr->callfuntion;
		}
		#endif
		//sysprintf("&%x-\n",tidptr->callfuntion);
  	 }
}

#endif

TimerId_id mytimer_create(const char *pname)
{
	TimerId_id tid;
	uint32_t len;

	if(!pname){
		TIMER_LOG3("mytimer None name!\n");
		return NULL;
	}

	len = strlen(pname);

	if(len == 0){	
		TIMER_LOG3("mytimer name len?\n");
		return NULL;
	}
	
	tid = OslMalloc(sizeof(TimerId_t));

	if(tid == NULL){
		TIMER_LOG3("mytimer_create malloc err\n");
		return NULL;
	}
	
	memset(tid,0,sizeof(TimerId_t));
	
	strncpy(tid->pname,pname,MYTIMER_MAME_LEN_MAX);

	#if defined(__FreeRTOS__)
	
	#if !defined(ENABLE_MYTIMER_HW)
	tid->tid = xTimerCreate((const char*	 )tid->pname,
											 (TickType_t		 )1000,
											 (UBaseType_t		 )pdFALSE,
											 (void* 			 )tid,
											 (TimerCallbackFunction_t)timer_commom_callback); 

	 if (tid->tid == NULL){
        TIMER_LOG3("timer_create err:%s\n",pname);		
		OslMfree(tid);
		return NULL;
	 }
	 #endif

	 #elif defined(__RTT_OS__)

	 rt_uint8_t  flag;
	 
	#if defined(ENABLE_MYTIMER_HW)
	flag = RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_TIMER;
	#else
	flag = RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_SOFT_TIMER;
	#endif

	 tid->tid =  rt_timer_create((const char*	 )tid->pname,
					timer_commom_callback,(void* )tid,1000,flag);

	 if (tid->tid == RT_NULL){
        TIMER_LOG3("timer_create err:%s\n",pname);		
		OslMfree(tid);
		return NULL;
	 }
	 

	 #endif

	 //TIMER_LOG("mytimer:%s\n",tid->pname);

	 return tid;
}


#if defined(__RTT_OS__)
TimerId_id mytimer_hw_create(const char *pname)
{
	TimerId_id tid;
	uint32_t len;
	rt_uint8_t	flag;

	if(!pname){
		TIMER_LOG3("mytimer None name!\n");
		return NULL;
	}

	len = strlen(pname);

	if(len == 0){	
		TIMER_LOG3("mytimer name len?\n");
		return NULL;
	}
	
	tid = OslMalloc(sizeof(TimerId_t));

	if(tid == NULL){
		TIMER_LOG3("mytimer_create malloc err\n");
		return NULL;
	}
	
	memset(tid,0,sizeof(TimerId_t));
	
	strncpy(tid->pname,pname,MYTIMER_MAME_LEN_MAX);	
	 
	flag = RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_TIMER;


	 tid->tid =  rt_timer_create((const char*	 )tid->pname,
					timer_commom_callback,(void* )tid,1000,flag);

	 if (tid->tid == RT_NULL){
        TIMER_LOG3("timer_create err:%s\n",pname);		
		OslMfree(tid);
		return NULL;
	 }
	 

	 //TIMER_LOG("mytimer:%s\n",tid->pname);

	 return tid;
}


#endif



#if defined(ENABLE_FREERTOS_SYSAPI)
int mytimer_open(TimerId_id tid,unsigned int secs,unsigned int ms,
	kal_timer_func_ptr cfun,void *param,bool isauto)
{
	BaseType_t ret;

	//ASSERT(!systemIsISR());

	if(tid == NULL)
		return -1;

	ms += secs * 1000;	

	#if defined(ENABLE_MYTIMER_HW)
		
	my_HW_timer_stop(tid->tid);	

	tid->callfuntion = cfun;
	tid->param_ptr = param;
	
	tid->tid = my_HW_timer_start(kal_milli_secs_to_ticks(ms),cfun,param,isauto);

	if(tid->tid == 0){
		TIMER_LOG3("timer 1start err:%s\n",tid->pname);
		return -1;
	}
	
	#else

	///if(xTimerIsTimerActive(tid->tid)){

	//	ret = xTimerStop(tid->tid,MYTIMER_STOP_TIMEOUT);//xTimerStopFromISR(tid->tid,NULL) 
	//}
	
	tid->callfuntion = cfun;
	tid->param_ptr = param;
	
	if(isauto)
		tid->tid->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
	else
		tid->tid->ucStatus &= ~tmrSTATUS_IS_AUTORELOAD;
	
	//vTimerSetReloadMode(tid->tid,isauto);
	
	tid->tid->xTimerPeriodInTicks = kal_milli_secs_to_ticks(ms); // xTimerChangePeriod

	ret = xTimerStart(tid->tid,MYTIMER_START_TIMEOUT); //xTimerStartFromISR(tid->tid,NULL) 
	if(ret == pdFAIL){
		TIMER_LOG3("timer 1start err:%s\n",tid->pname);
		return -1;
	}
	#endif

	return 0;
}


int mytimer_open_v2(TimerId_id tid,unsigned int ms,
	kal_timer_func_ptr cfun,void *param,bool isauto)
{
	BaseType_t ret;

	//ASSERT(!systemIsISR());

	if(tid == NULL)
		return -1;	
	
	#if defined(ENABLE_MYTIMER_HW)
	
	my_HW_timer_stop(tid->tid);	

	tid->callfuntion = cfun;
	tid->param_ptr = param;
	
	tid->tid = my_HW_timer_start(kal_milli_secs_to_ticks(ms),cfun,param,isauto);

	if(tid->tid == 0){
		TIMER_LOG3("timer 2start err:%s\n",tid->pname);
		return -1;
	}
	
	#else

	//if(xTimerIsTimerActive(tid->tid)){
	//	ret = xTimerStop(tid->tid,MYTIMER_STOP_TIMEOUT);//xTimerStopFromISR(tid->tid,NULL) 
	//}

	tid->callfuntion = cfun;
	tid->param_ptr = param;
	
	if(isauto)
		tid->tid->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
	else
		tid->tid->ucStatus &= ~tmrSTATUS_IS_AUTORELOAD;
	
	//vTimerSetReloadMode(tid->tid,isauto);
	
	tid->tid->xTimerPeriodInTicks = kal_milli_secs_to_ticks(ms); //ms/portTICK_PERIOD_MS	
	
	ret = xTimerStart(tid->tid,MYTIMER_START_TIMEOUT);//xTimerStartFromISR(tid->tid,NULL) 
	
	if(ret == pdFAIL){
		TIMER_LOG3("timer 2start err:%s\n",tid->pname);
		//delay_ms(20);
		//ASSERT(0);
		return -1;
	}
	#endif

	return 0;
}


int mytimer_open_isr(TimerId_id tid,unsigned int secs,unsigned int ms,
	kal_timer_func_ptr cfun,void *param,bool isauto)
{
	BaseType_t ret;

	if(tid == NULL)
		return -1;

	ms += secs * 1000;

	#if defined(ENABLE_MYTIMER_HW)
	my_HW_timer_stop(tid->tid);	

	tid->callfuntion = cfun;
	tid->param_ptr = param;
	
	tid->tid = my_HW_timer_start(kal_milli_secs_to_ticks(ms),cfun,param,isauto);

	if(tid->tid == 0){
		TIMER_LOG3("timer 1start isr err:%s\n",tid->pname);
		return -1;
	}
	
	#else

	
	//if(xTimerIsTimerActiveFromISR(tid->tid)){
		//	ret = xTimerStopFromISR(tid->tid,NULL); 		
		//}
		
	tid->callfuntion = cfun;
	tid->param_ptr = param;
	
	if(isauto)
		tid->tid->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
	else
		tid->tid->ucStatus &= ~tmrSTATUS_IS_AUTORELOAD;
	
	//vTimerSetReloadMode(tid->tid,isauto);
	
	tid->tid->xTimerPeriodInTicks = kal_milli_secs_to_ticks(ms); // xTimerChangePeriod



	#ifdef ENABLE_MYTIMER_TASK
	ret =  mytimer_task_isr_enqueue_msg(tid,ms,cfun,param,isauto);
	#else
	ret = xTimerStartFromISR(tid->tid,NULL);
	#endif

	if(ret == pdFAIL){
		TIMER_LOG3("timer 1start isr err:%s\n",tid->pname);
		return -1;
	}
	#endif

	return 0;
}


int mytimer_open_v2_isr(TimerId_id tid,unsigned int ms,
	kal_timer_func_ptr cfun,void *param,bool isauto)
{
	BaseType_t ret;

	if(tid == NULL)
		return -1;

	#if defined(ENABLE_MYTIMER_HW)
		
	my_HW_timer_stop(tid->tid);	
	
	tid->callfuntion = cfun;
	tid->param_ptr = param;
	
	tid->tid = my_HW_timer_start(kal_milli_secs_to_ticks(ms),cfun,param,isauto);

	if(tid->tid == 0){
		TIMER_LOG3("timer 2start isr err:%s\n",tid->pname);
		return -1;
	}
	
	#else

	//if(xTimerIsTimerActiveFromISR(tid->tid)){
	//	ret = xTimerStopFromISR(tid->tid,NULL);			
	//}

	tid->callfuntion = cfun;
	tid->param_ptr = param;	
	
	if(isauto)
		tid->tid->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
	else
		tid->tid->ucStatus &= ~tmrSTATUS_IS_AUTORELOAD;
	
	//vTimerSetReloadMode(tid->tid,isauto);
	
	tid->tid->xTimerPeriodInTicks = kal_milli_secs_to_ticks(ms); //ms/portTICK_PERIOD_MS	
	
	#ifdef ENABLE_MYTIMER_TASK
	ret =  mytimer_task_isr_enqueue_msg(tid,ms,cfun,param,isauto);
	#else
	ret = xTimerStartFromISR(tid->tid,NULL);
	#endif

	if(ret == pdFAIL){
		TIMER_LOG3("timer 2start isr err:%s\n",tid->pname);
		//delay_ms(20);
		//ASSERT(0);
		return -1;
	}
	#endif

	return 0;
}


#else

int mytimer_open(TimerId_id tid,unsigned int secs,unsigned int ms,
	kal_timer_func_ptr cfun,void *param,bool isauto)
{
#if defined(__FreeRTOS__)

	BaseType_t ret;
	bool isISR;

	if(tid == NULL)
		return -1;

	ms += secs * 1000;

	#if defined(ENABLE_MYTIMER_HW)
	
	my_HW_timer_stop(tid->tid);	

	tid->callfuntion = cfun;
	tid->param_ptr = param;
	
	tid->tid = my_HW_timer_start(kal_milli_secs_to_ticks(ms),cfun,param,isauto);

	if(tid->tid == 0){
		TIMER_LOG3("timer 1start err:%s\n",tid->pname);
		return -1;
	}
	
	#else

	isISR = systemIsISR();


	if(xTimerIsTimerActive_custom(tid->tid,isISR)){

		if(isISR)
			ret = xTimerStopFromISR(tid->tid,NULL);			
		else
			ret = xTimerStop(tid->tid,MYTIMER_STOP_TIMEOUT);//xTimerStopFromISR(tid->tid,NULL) 
	}
	
	tid->callfuntion = cfun;
	tid->param_ptr = param;
	
	if(isauto)
		tid->tid->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
	else
		tid->tid->ucStatus &= ~tmrSTATUS_IS_AUTORELOAD;
	
	//vTimerSetReloadMode(tid->tid,isauto);
	
	tid->tid->xTimerPeriodInTicks = kal_milli_secs_to_ticks(ms); // xTimerChangePeriod


	if(isISR)
		#ifdef ENABLE_MYTIMER_TASK
		ret =  mytimer_task_isr_enqueue_msg(tid,ms,cfun,param,isauto);
		#else
		ret = xTimerStartFromISR(tid->tid,NULL);
		#endif
	else	
		ret = xTimerStart(tid->tid,MYTIMER_START_TIMEOUT); //xTimerStartFromISR(tid->tid,NULL) 
	if(ret == pdFAIL){
		TIMER_LOG3("timer 1start err:%s,%d\n",tid->pname,isISR);
		return -1;
	}
	#endif

#elif defined(__RTT_OS__)

	if(tid == NULL)
		return -1;


	rt_timer_stop(tid->tid);

	ms += secs * 1000;


	tid->callfuntion = cfun;
	tid->param_ptr = param;

	rt_timer_control(tid->tid,RT_TIMER_CTRL_SET_TIME,&ms);

	if(isauto)
		rt_timer_control(tid->tid,RT_TIMER_CTRL_SET_PERIODIC,NULL);
	else
		rt_timer_control(tid->tid,RT_TIMER_CTRL_SET_ONESHOT,NULL);

	if(rt_timer_start(tid->tid) != RT_EOK){
		TIMER_LOG3("timer 2start err:%s\n",tid->pname);
		//delay_ms(20);
		//ASSERT(0);
		return -1;
	}
		

#endif	

	return 0;
}


int mytimer_open_v2(TimerId_id tid,unsigned int ms,
	kal_timer_func_ptr cfun,void *param,bool isauto)
{

#if defined(__FreeRTOS__)

	BaseType_t ret;
	bool isISR;

	if(tid == NULL)
		return -1;

	#if defined(ENABLE_MYTIMER_HW)
	
	my_HW_timer_stop(tid->tid);	

	tid->callfuntion = cfun;
	tid->param_ptr = param;
	
	tid->tid = my_HW_timer_start(kal_milli_secs_to_ticks(ms),cfun,param,isauto);

	if(tid->tid == 0){
		TIMER_LOG3("timer 1start err:%s\n",tid->pname);
		return -1;
	}
	
	#else

	isISR = systemIsISR();

	if(xTimerIsTimerActive_custom(tid->tid,isISR)){
		if(isISR)
			ret = xTimerStopFromISR(tid->tid,NULL);			
		else
			ret = xTimerStop(tid->tid,MYTIMER_STOP_TIMEOUT);//xTimerStopFromISR(tid->tid,NULL) 
	}

	tid->callfuntion = cfun;
	tid->param_ptr = param;
	
	
	if(isauto)
		tid->tid->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
	else
		tid->tid->ucStatus &= ~tmrSTATUS_IS_AUTORELOAD;
	
	//vTimerSetReloadMode(tid->tid,isauto);
	
	tid->tid->xTimerPeriodInTicks = kal_milli_secs_to_ticks(ms); //ms/portTICK_PERIOD_MS	
	

	if(isISR)
		#ifdef ENABLE_MYTIMER_TASK
		ret =  mytimer_task_isr_enqueue_msg(tid,ms,cfun,param,isauto);
		#else
		ret = xTimerStartFromISR(tid->tid,NULL);
		#endif
	else
		ret = xTimerStart(tid->tid,MYTIMER_START_TIMEOUT);//xTimerStartFromISR(tid->tid,NULL) 
	if(ret == pdFAIL){
		TIMER_LOG3("timer 2start err:%s,%d\n",tid->pname,isISR);
		//delay_ms(20);
		//ASSERT(0);
		return -1;
	}
	#endif

#elif defined(__RTT_OS__)

	if(tid == NULL)
		return -1;


	rt_timer_stop(tid->tid);


	tid->callfuntion = cfun;
	tid->param_ptr = param;

	rt_timer_control(tid->tid,RT_TIMER_CTRL_SET_TIME,&ms);

	if(isauto)
		rt_timer_control(tid->tid,RT_TIMER_CTRL_SET_PERIODIC,NULL);
	else
		rt_timer_control(tid->tid,RT_TIMER_CTRL_SET_ONESHOT,NULL);

	if(rt_timer_start(tid->tid) != RT_EOK){
		TIMER_LOG3("timer 2start err:%s\n",tid->pname);
		//delay_ms(20);
		//ASSERT(0);
		return -1;
	}
		

#endif
	

	return 0;
}
#endif



#ifdef ENABLE_MYTIMER_TASK
int mytimer_open_task(TimerId_id tid,unsigned int ms,
	kal_timer_func_ptr cfun,void *param,bool isauto)
{
	BaseType_t ret;


	if(tid == NULL)
		return -1;


	if(xTimerIsTimerActive(tid->tid))
		ret = xTimerStop(tid->tid,MYTIMER_STOP_TIMEOUT);//xTimerStopFromISR(tid->tid,NULL) 

	tid->callfuntion = cfun;
	tid->param_ptr = param;
	
	
	if(isauto)
		tid->tid->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
	else
		tid->tid->ucStatus &= ~tmrSTATUS_IS_AUTORELOAD;
		
	//vTimerSetReloadMode(tid->tid,isauto);
	
	tid->tid->xTimerPeriodInTicks = kal_milli_secs_to_ticks(ms); //ms/portTICK_PERIOD_MS	
	
	/*
	ret = xTimerChangePeriod(tid->tid,ms,10);
	if(ret == pdFAIL){
		TIMER_LOG("timer change err");
		return -1;
	}
	*/

	ret = xTimerStart(tid->tid,MYTIMER_START_TIMEOUT);//xTimerStartFromISR(tid->tid,NULL) 
	if(ret == pdFAIL){
		TIMER_LOG3("timer op task err:%s\n",tid->pname);
		//delay_ms(20);
		ASSERT(0);
		return -1;
	}

	return 0;
}
#endif

int mytimer_open_us(TimerId_id tid,long us,kal_timer_func_ptr cfun,void *param,bool isauto)
{
	long ms = us / 1000;
	
	if(ms == 0)
		ms = 1;

	return mytimer_open_v2(tid,ms,cfun,param,isauto);
}




#if defined(ENABLE_FREERTOS_SYSAPI)

int mytimer_close(TimerId_id tid)
{
	BaseType_t ret;

	//ASSERT(!systemIsISR());

	if(tid == NULL)
		return -1;	


	#if defined(ENABLE_MYTIMER_HW)

	my_HW_timer_stop(tid->tid);	
	tid->tid = 0;
	
	#else	

	if(xTimerIsTimerActive(tid->tid)){

		ret =xTimerStop(tid->tid,MYTIMER_STOP_TIMEOUT);
		
		if(ret == pdFAIL){
			TIMER_LOG3("timer stop err:%s\n",tid->pname);
			return -1;
		}
	}
	#endif
	
	return 0;
}

int mytimer_close_isr(TimerId_id tid)
{
	BaseType_t ret;

	if(tid == NULL)
		return -1;	


	#if defined(ENABLE_MYTIMER_HW)

	my_HW_timer_stop(tid->tid);	
	tid->tid = 0;
	
	#else
	if(xTimerIsTimerActiveFromISR(tid->tid)){

		ret = xTimerStopFromISR(tid->tid,NULL);
	
		if(ret == pdFAIL){
			TIMER_LOG3("timer stop err:%s\n",tid->pname);
			return -1;
		}
	}
	#endif
	
	return 0;
}


#else
int mytimer_close(TimerId_id tid)
{

#if defined(__FreeRTOS__)

	BaseType_t ret;
	bool isISR;

	if(tid == NULL)
		return -1;	

	#if defined(ENABLE_MYTIMER_HW)

	my_HW_timer_stop(tid->tid);	
	tid->tid = 0;
	
	#else

	isISR = systemIsISR();

	if(xTimerIsTimerActive_custom(tid->tid,isISR)){

		if(isISR)
			ret = xTimerStopFromISR(tid->tid,NULL);
		else
			ret =xTimerStop(tid->tid,MYTIMER_STOP_TIMEOUT);
		
		if(ret == pdFAIL){
			TIMER_LOG3("timer stop err:%s\n",tid->pname);
			return -1;
		}
	}
	#endif

#elif defined(__RTT_OS__)

	if(tid == NULL)
		return -1;

	rt_timer_stop(tid->tid);

#endif
	
	return 0;
}
#endif
int mytimer_free(TimerId_id *tid)
{
	
	
	if(tid == NULL || *tid == NULL)
		return -2;

#if defined(__FreeRTOS__)
	BaseType_t ret;

	#if defined(ENABLE_MYTIMER_HW)

	my_HW_timer_stop((*tid)->tid);
	
	#else
	ret = xTimerDelete((*tid)->tid,MYTIMER_STOP_TIMEOUT);
	if(ret == pdFAIL){
		TIMER_LOG3("timer free err\n");
		return -1;
	}
	#endif

#elif defined(__RTT_OS__)
	rt_timer_delete((*tid)->tid);
#endif


	OslMfree(*tid);
	*tid = NULL;
	return 0;
}

#if defined(__PC_SIM__)

bool systemIsISR(void)
{
	return false;
}

#else


#if defined(__FreeRTOS__)

//andyli 20191206   N32926 HW timer  --> GZLL Timer ;     timerID > 0
int my_HW_timer_start(UINT32 tick,PVOID pfun,void *param,BOOL isAuto)
{
	kal_uint32 savedMask;
	int ret;
	
	savedMask = rt_hw_interrupt_disable();
	ret = sysSetTimerEvent_custom(TIMER0, tick, pfun,param,isAuto);
	rt_hw_interrupt_enable(savedMask);
	
	if(ret == -1)
		return 0;
	else
		return ret;
}

int my_HW_timer_stop(int timerID)
{
	kal_uint32 savedMask;
	
	if(timerID <= 0)
		return -1;

	savedMask = rt_hw_interrupt_disable();
	sysClearTimerEvent(TIMER0, timerID);
	rt_hw_interrupt_enable(savedMask);

	return 0;
}


#define TIMER3_HZ      1000000  //1MHZ

void hw_delay_us(uint32_t us)
{

	UINT32 u32ExtFreq;
	
	/* init timer */	
	u32ExtFreq = sysGetExternalClock();
	sysSetTimerReferenceClock (TIMER3, u32ExtFreq);
	sysStartTimer(TIMER3, TIMER3_HZ , PERIODIC_MODE);	
	
}
#endif

#endif
